package com.apps.stoolkit;

import com.apps.saspect.SuportLockModel;
import com.apps.sconfig.SDevFastConfig;
import com.apps.sconfig.enums.LockType;
import com.apps.sworld.sToolkit;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.*;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * redisson帮助类
 */
@Slf4j
@Component
public class RedissonToolkit implements sToolkit {

    @Autowired
    private SDevFastConfig sDevFastConfig;


    @Override
    public void onLoad()
    {
        if(sDevFastConfig.getLockType() == LockType.REDISSON)
        {
            try
            {
               getRedissonClient();
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }

    public void getRedissonClient() throws IOException {

        Config config =ApplicationContextToolkit.getApplicationContext().getBean(RedissonClient.class).getConfig();
        log.info(config.toJSON().toString());
    }

    /**`
     * 获取字符串对象
     *
     * @param objectName
     * @return
     */
    public static  <T> RBucket<T> getRBucket(String objectName) {
        RBucket<T> bucket = ApplicationContextToolkit.getApplicationContext().getBean(RedissonClient.class).getBucket(objectName);
        return bucket;
    }

    /**
     * 获取Map对象
     *
     * @param objectName
     * @return
     */
    public static <K, V> RMap<K, V> getRMap(String objectName) {
        RMap<K, V> map = ApplicationContextToolkit.getApplicationContext().getBean(RedissonClient.class).getMap(objectName);
        return map;
    }

    /**
     * 获取有序集合
     *
     * @param objectName
     * @return
     */
    public static <V> RSortedSet<V> getRSortedSet(String objectName) {
        RSortedSet<V> sortedSet = ApplicationContextToolkit.getApplicationContext().getBean(RedissonClient.class).getSortedSet(objectName);
        return sortedSet;
    }

    /**
     * 获取集合
     *
     * @param objectName
     * @return
     */
    public static <V> RSet<V> getRSet(String objectName) {
        RSet<V> rSet = ApplicationContextToolkit.getApplicationContext().getBean(RedissonClient.class).getSet(objectName);
        return rSet;
    }

    /**
     * 获取列表
     *
     * @param objectName
     * @return
     */
    public static <V> RList<V> getRList(String objectName) {
        RList<V> rList = ApplicationContextToolkit.getApplicationContext().getBean(RedissonClient.class).getList(objectName);
        return rList;
    }
    /**
     * 获取队列
     *
     * @param objectName
     * @return
     */
    public static <V> RQueue<V> getRQueue(String objectName) {
        RQueue<V> rQueue = ApplicationContextToolkit.getApplicationContext().getBean(RedissonClient.class).getQueue(objectName);
        return rQueue;
    }

    /**
     * 获取双端队列
     *
     * @param objectName
     * @return
     */
    public static <V> RDeque<V> getRDeque(String objectName) {
        RDeque<V> rDeque = ApplicationContextToolkit.getApplicationContext().getBean(RedissonClient.class).getDeque(objectName);
        return rDeque;
    }


    /**
     * 获取锁
     *
     * @param objectName
     * @return
     */
    @SuportLockModel(type = LockType.REDISSON)
    public static RLock getRLock(String objectName) {
        RLock rLock = ApplicationContextToolkit.getApplicationContext().getBean(RedissonClient.class).getLock(objectName);
        return rLock;
    }

    /**
     * 公平锁
     * @param objectName
     * @return
     */
    @SuportLockModel(type = LockType.REDISSON)
    public static RLock getFairRLock(String objectName) {
        RLock rLock = ApplicationContextToolkit.getApplicationContext().getBean(RedissonClient.class).getFairLock(objectName);
        return rLock;
    }

    /**
     * 获取读取锁
     *
     * @param objectName
     * @return
     */
    public static RReadWriteLock getRWLock(String objectName) {
        RReadWriteLock rwlock = ApplicationContextToolkit.getApplicationContext().getBean(RedissonClient.class).getReadWriteLock(objectName);
        return rwlock;
    }

    /**
     * 获取原子数
     * @param objectName
     * @return
     */
    public static RAtomicLong getRAtomicLong(String objectName) {
        RAtomicLong rAtomicLong = ApplicationContextToolkit.getApplicationContext().getBean(RedissonClient.class).getAtomicLong(objectName);
        return rAtomicLong;
    }

    /**
     * 获取记数锁
     *
     * @param objectName
     * @return
     */
    public static RCountDownLatch getRCountDownLatch(String objectName) {
        RCountDownLatch rCountDownLatch = ApplicationContextToolkit.getApplicationContext().getBean(RedissonClient.class).getCountDownLatch(objectName);
        return rCountDownLatch;
    }


}
